/*
 *
 *  Copyright (C) 2010 GSyC/LibreSoft, Universidad Rey Juan Carlos.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Raúl Román López <rroman@gsyc.es>
 *
 */

package com.LibreGeoSocial.App.ARTesting;

import java.util.List;

import android.location.Location;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.LibreGeoSocial.App.R;
import com.LibreGeoSocial.App.GeoNames.AltitudeManager;
import com.LibreGeoSocial.App.Overlay.ResourceOverlay;
import com.LibreGeoSocial.App.Utils.MapUtils;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class RealLocation extends MapActivity{
	
	private MapView map;
	private EditText etxt;
	private GeoPoint map_center;
	private boolean capture;
	
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.location);
        
        map = (MapView)findViewById(R.id.locationMap);
//        map = new MapView(this, getString(R.string.map_key));
        //Adding a zoom control
        LinearLayout zoomLayout = (LinearLayout) findViewById(R.id.location_layout_zoom);
		View zoomView = map.getZoomControls();
		zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		
        map.getController().setCenter(new GeoPoint(
        		(int)(TestingManager.getUserLocation().getLatitude() * 1E6), 
        		(int)(TestingManager.getUserLocation().getLongitude() * 1E6)));
        map.getController().setZoom(18);
        
        map.setClickable(true);
        map.setSatellite(true);
        //FIXME apañar esto del zoom
        //map.setBuiltInZoomControls(true);
        map.displayZoomControls(true);
        map.setOnTouchListener(touchListener);
        
        setPositionInMap(TestingManager.getResRealLocation());
        
//        setContentView(map);
        
        LinearLayout ll = new LinearLayout(this);
        
        etxt = new EditText(this);
        etxt.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
        etxt.setHint("Enter Height");
        ll.addView(etxt, new LayoutParams(150,LayoutParams.WRAP_CONTENT));
        
        Button bt = new Button(this);
        bt.setText("Ok");
        bt.setOnClickListener(clickListener);
        ll.addView(bt, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        
        addContentView(ll, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
    }
	
	OnClickListener clickListener = new OnClickListener(){

		public void onClick(View v) {
			if(TestingManager.getResRealLocation() != null){
				float altitude = (float) AltitudeManager.getAltitudeFromLatLong((float)TestingManager.getResRealLocation().getLatitude(), 
						(float)TestingManager.getResRealLocation().getLongitude());
				
				try{
					float h = Float.parseFloat(etxt.getText().toString());
					altitude += h;
					Log.d("RealLocation", "altitude");
				}catch(Exception e){
					Log.e("RealLocation", "e");
				}
				TestingManager.getResRealLocation().setAltitude(altitude);
			}
		}
		
	};
	
    private OnTouchListener touchListener = new OnTouchListener(){

		public boolean onTouch(View v, MotionEvent event) {
			
			switch(event.getAction()){
			
			case MotionEvent.ACTION_DOWN:

				map_center = map.getMapCenter();
				capture = true;
				break;
				
			case MotionEvent.ACTION_MOVE:
		    	GeoPoint new_map_center = map.getMapCenter();
		    	
		    	double distance = Math.sqrt(Math.pow(new_map_center.getLatitudeE6()-map_center.getLatitudeE6(),2)+
		    			Math.pow(new_map_center.getLongitudeE6()-map_center.getLongitudeE6(),2));
		    	
		    	map_center = new_map_center;
		    	
		    	if(distance > 1)
		    		capture = false;
		    	
				break;
			
			case MotionEvent.ACTION_UP:
				if (!capture)
					break;
				capture = false;
				
				Location loc = MapUtils.getLocFromXY(event.getX(), event.getY(), map, 0);
				float altitude = (float) AltitudeManager.getAltitudeFromLatLong((float)loc.getLatitude(), 
						(float)loc.getLongitude());
				
				try{
					float h = Float.parseFloat(etxt.getText().toString());
					altitude += h;
					Log.d("RealLocation", "altitude");
				}catch(Exception e){
					Log.e("RealLocation", "e");
				}
				
				TestingManager.setResRealLoc((float)loc.getLatitude(), (float)loc.getLongitude(), altitude);
				setPositionInMap(loc);
				
				break;
				
			default:
					break;
			}
			
			return false;
		}
    };
    
	private void setPositionInMap(Location loc){
		
        ResourceOverlay myposOverlay = new ResourceOverlay("User");
        myposOverlay.setLocation(TestingManager.getUserLocation());
        
        map.getOverlays().clear();
	    final List<Overlay> overlays = map.getOverlays();	   
	    overlays.add( myposOverlay );
	    
	    if(loc != null){

	    	ResourceOverlay resposOverlay = new ResourceOverlay("Real res");
	    	resposOverlay.setLocation(loc);
	    	overlays.add(resposOverlay);
	    }
	    map.invalidate();
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	
	
}